package com.syh.jn.scm.domain.downstream.entity.finance;

import cn.redscarf.commons.util.bean.ExtBeanUtils;
import cn.redscarf.commons.util.other.ExtAssert;
import cn.redscarf.commons.util.validator.ValidationUtils;
import com.syh.core.ddd.domain.AbstractSimpleAggregate;
import com.syh.jn.scm.dto.downstream.finance.DownCostBillSto;
import com.syh.jn.scm.dto.downstream.finance.DownCostBillVo;
import com.syh.jn.scm.dto.support.FilesDto;
import com.syh.jn.scm.exception.BusinessException;
import com.syh.jn.scm.service.api.downstream.finance.enums.DownCostBillEnum;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.math.BigDecimal;
import java.util.UUID;

/**
 * Date:2018/7/19
 * Author:Clown
 * 下游费用单业务模型;
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class DownCostBill extends AbstractSimpleAggregate<Long> {
  /**
   * 费用单编号
   */
  private String code;
  /**
   * 客户id
   */
  private Long customerId;
  /**
   * 类型
   */
  private Short customerType;
  /**
   * 金额
   */
  private BigDecimal price;
  /**
   * 归属经营主体
   */
  private Long businessEntityId;
  /**
   * 创建时间
   */
  private Long inputTimestamp;
  /**
   * 录单人id
   */
  private Long brokerageId;
  /**
   * 费用单状态:0待确认, 1已确认, 2已生成账单, -1已取消
   */
  private Short state;
  /**
   * 对方确认生效状态：0未生效, 1已生效
   */
  private Short confirmState;
  /**
   * 备注
   */
  private String remark;
  /**
   * 对账单id
   */
  private Long statementOfAccountId;
  /**
   * 下游单据图片
   */
  private String secondSrcImages;

  /**
   * 创建费用单
   *
   * @param sto
   * @return
   */
  public static DownCostBill createCostBill(DownCostBillSto sto) {
    ExtAssert.notNull(sto, "对象为空");
    ValidationUtils.validate(sto);
    DownCostBill bill = DownCostBill.systemCreate(sto);
    //TODO 生成code编号
    bill.setUuid(UUID.randomUUID().toString());
    return bill;
  }

  /**
   * 系统默认生成字段
   *
   * @return
   */
  public static DownCostBill systemCreate(DownCostBillSto sto) {
    DownCostBill bill = new DownCostBill();
    ExtBeanUtils.copyProperties(sto, bill);
    bill.state = DownCostBillEnum.STAYCONFIRM.getStatus();
    bill.createTimeStamp = System.currentTimeMillis();
    bill.confirmState = DownCostBillEnum.OTHERPARTY_STAYCONFIRM.getStatus();
    return bill;
  }

  /**
   * 确认生效
   */
  public void confirmState() {
    this.confirmState = DownCostBillEnum.OTHERPARTY_CONFIRM.getStatus();
  }

  /**
   * 取消确认生效
   */
  public void cancelConfirm() {
    if (this.state.equals(DownCostBillEnum.STATEMENT.getStatus())) {
      throw new BusinessException("费用单已生产对账单，无法取消确认！!");
    }
    this.confirmState = DownCostBillEnum.OTHERPARTY_STAYCONFIRM.getStatus();
  }

  /**
   * 生成对账单
   */
  public void generating(Long statementOfAccountId) {
    if (this.state.equals(DownCostBillEnum.STATEMENT.getStatus())) {
      throw new BusinessException("费用单已生成对账单!");
    }
    this.statementOfAccountId = statementOfAccountId;
    this.state = DownCostBillEnum.STATEMENT.getStatus();
  }

  /**
   * 取消生成对账单
   */
  public void cancelGenerating() {
    if (!this.state.equals(DownCostBillEnum.STATEMENT.getStatus())) {
      throw new BusinessException("费用单已取消对账单!");
    }
    this.statementOfAccountId = null;
    this.state = DownCostBillEnum.CONFIRM.getStatus();
  }

  public DownCostBill() {
  }

  public DownCostBill(DownCostBillVo vo) {
    ExtAssert.notNull(vo, "对象为空");
    if (vo.getId() == null)
      throw new BusinessException("缺少必要参数");
    ExtBeanUtils.copyProperties(vo, this);
  }

  /**
   * 上传图片
   *
   * @param dto
   */
  public void upload(FilesDto dto) {
    this.secondSrcImages = dto.getFiles();
  }
}
